﻿using MailKit.Net.Smtp;
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mailtool
{
    class Program
    {
        static void Main(string[] args)
        {
            //System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            //try
            //{
            //    mail.To = "gisdaodao123@126.com";
            //    mail.From = "15952003521@qq.com";
            //    mail.Subject = "这是主题";
            //    mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            //    mail.Body = "这是内容";

            //    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证  
            //    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //邮箱登录账号，这里跟前面的发送账号一样就行  
            //    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "rotouamqtjoubeha"); //这个密码要注意：如果是一般账号，要用授权码；企业账号用登录密码  
            //    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口  
            //    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密  
            //    System.Web.Mail.SmtpMail.SmtpServer = "smtp.qq.com";    //企业账号用smtp.exmail.qq.com  
            //    System.Web.Mail.SmtpMail.Send(mail);

            //    //邮件发送成功  
            //}
            //catch (Exception ex)
            //{
            //    //失败，错误信息：ex.Message;  
            //}
            SentEmail(@"d:\documents\visual studio 2017\Projects\word-xps-mongodb\mailtool\huigou128.png");
        }
        public static void SentEmail(string path)
        {
            var message = new MimeMessage();
            //获取From标头中的地址列表，添加指定的地址
            message.From.Add(new MailboxAddress("chao", "1020684770@qq.com"));
            //获取To头中的地址列表，添加指定的地址
            message.To.Add(new MailboxAddress("gis", "gisdaodao123@126.com"));
            message.To.Add(new MailboxAddress("to", "1020684770@qq.com"));
            message.To.Add(new MailboxAddress("youyouchina@hotmail.com", "youyouchina@hotmail.com"));
            //获取或设置消息的主题
            message.Subject = "How you doin?";
            // 创建我们的消息文本，就像以前一样（除了不设置为message.Body）
            var body = new TextPart("plain")
            {
                Text = @"hi it is zhangchao'greeting"
            };
            // 为位于路径的文件创建图像附件
            var attachment = new MimePart("image", "gif")
            {
                ContentObject = new ContentObject(File.OpenRead(path), ContentEncoding.Default),
                ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName = Path.GetFileName(path)
            };
            // 现在创建multipart / mixed容器来保存消息文本和图像附件
            var multipart = new Multipart("mixed")
            {
                body, attachment
            };
            // 现在将multipart / mixed设置为消息正文 
            message.Body = multipart;
            using (var client = new SmtpClient())
            {
                // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                client.Connect("smtp.qq.com", 25, false);

                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("15952003521@qq.com", "rotouamqtjoubeha");
            
                client.Send(message);
                client.Disconnect(true);
            }
        }
    }
}
